import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import javax.swing.JLabel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JToolBar;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JSeparator;

/**
 * @author edhem.brakmic
 * cccc
 */
public class DataCollector {

	private JFrame frame;
	private static String[] argsG;
	private static boolean end;
	private ListenerThread t;
	private JTable table;
	private JList list_1;
	private boolean outputOnOff;

	/**dd
	 * Launch the application.
	 */

	public static void main(String[] args) {

		argsG = new String[2];

		argsG[0] = "-comm";
		argsG[1] = "serial@/dev/ttyUSB1:iris";

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DataCollector window = new DataCollector();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DataCollector() {
		initialize();

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		outputOnOff = true;
		final DbConnector db = new DbConnector();
		frame = new JFrame();
		frame.setBounds(100, 100, 1095, 830);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(12, 612, 935, 163);

		final JTextArea textArea = new JTextArea(10, 83);
		panel.add(textArea);
		textArea.setWrapStyleWord(true);

		JScrollPane areaScrollPane = new JScrollPane(textArea);
		areaScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		panel.add(areaScrollPane);

		frame.getContentPane().add(panel);

		JLabel lblDataCollector = new JLabel("Data Collector");
		lblDataCollector.setBounds(470, 21, 97, 35);
		frame.getContentPane().add(lblDataCollector);

		JButton btnClear = new JButton("Clear");
		btnClear.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				textArea.setText("");
			}
		});
		btnClear.setBounds(970, 739, 107, 27);
		frame.getContentPane().add(btnClear);

		try {
			list_1 = new JList(db.getTableNames().toArray());
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		list_1.setBounds(0, 0, 2, 6);
		frame.getContentPane().add(list_1);
		list_1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list_1.setLayoutOrientation(JList.VERTICAL);
		list_1.setVisibleRowCount(-1);

		JScrollPane scrollPane = new JScrollPane(list_1);
		scrollPane.setBounds(12, 68, 127, 532);
		frame.getContentPane().add(scrollPane);

		TableM data = null;

		try {
			if (list_1.getModel().getSize() != 0) {
				data = db.getTableData(list_1.getModel().getElementAt(0)
						.toString());
				table = new JTable(data.getRowData(), data.getColTyp());
			} else {
				table = new JTable();

			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		JScrollPane scrollPane_1 = new JScrollPane(table);
		scrollPane_1.setBounds(151, 68, 795, 532);
		frame.getContentPane().add(scrollPane_1);

		final JToggleButton tglbtnOutputOnoff = new JToggleButton("Output Off");

		tglbtnOutputOnoff.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (t != null) {
					t.outputToogle(!tglbtnOutputOnoff.isSelected());
				}
			}
		});
		tglbtnOutputOnoff.setFont(new Font("Dialog", Font.BOLD, 9));
		tglbtnOutputOnoff.setBounds(970, 621, 107, 25);
		frame.getContentPane().add(tglbtnOutputOnoff);

		JSeparator separator = new JSeparator();
		separator.setBounds(12, 54, 1065, 2);
		frame.getContentPane().add(separator);

		final JToggleButton tglbtnStart = new JToggleButton("Start");
		tglbtnStart.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				if (tglbtnStart.isSelected() == true) {
					t = new ListenerThread(argsG, textArea, outputOnOff);
					t.start();
				} else {
					t.done();
				}
			}
		});
		tglbtnStart.setBounds(970, 68, 107, 25);
		frame.getContentPane().add(tglbtnStart);

		JButton btnCreateTable = new JButton("Create Table");
		btnCreateTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				/*
				 * String[] x = new String[2]; x[0] = "srcId"; x[1] = "value";
				 * 
				 * String[] y = new String[2]; y[0] = "int"; y[1] = "int";
				 * 
				 * try { db.CreateTable("wsntest", x, y); } catch (SQLException
				 * e) { // TODO Auto-generated catch block e.printStackTrace();
				 * }
				 */

				NewTable tableWindow = new NewTable(list_1);
				tableWindow.frame.setVisible(true);

			}
		});
		btnCreateTable.setFont(new Font("Dialog", Font.BOLD, 10));
		btnCreateTable.setBounds(970, 105, 107, 25);
		frame.getContentPane().add(btnCreateTable);

		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				try {
					TableM dataChange = db.getTableData(list_1
							.getSelectedValue().toString());
					table.setModel(new DefaultTableModel(dataChange
							.getRowData(), dataChange.getColTyp()));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		btnRefresh.setBounds(970, 174, 107, 25);
		frame.getContentPane().add(btnRefresh);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(12, 607, 1065, 4);
		frame.getContentPane().add(separator_1);

		JButton btnDopTable = new JButton("Drop table");
		btnDopTable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// try {

				// I turn it of because it should be a message window to confirm
				// the action
				// db.dropTable(list_1.getSelectedValue().toString());
				// } catch (SQLException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				// }

				try {
					list_1.removeAll();
					list_1.setListData(db.getTableNames().toArray());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		btnDopTable.setBounds(970, 137, 107, 25);
		frame.getContentPane().add(btnDopTable);

		list_1.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {

				try {
					if (list_1.getSelectedValue() != null) {
						TableM dataChange = db.getTableData(list_1
								.getSelectedValue().toString());
						table.setModel(new DefaultTableModel(dataChange
								.getRowData(), dataChange.getColTyp()));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}
}